import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { NzMessageService } from 'ng-zorro-antd';
import { FetchService } from '../../../../core/net/fetch.service';

@Component({
    selector: 'app-sf-regular-check',
    templateUrl: './sf-regular-check.component.html',
    styleUrls: ['../sf-check.component.scss']
})
export class SfRegularCheckComponent implements OnInit {
    searchForm: FormGroup;
    tableData: Array<any> = [];
    pageNum = 1;
    pageSize = 10;
    total: number;
    loading = false;
    form: FormGroup;
    modalTitle: string = '';
    modalShow: boolean = false;
    queryType: "";

    constructor(private fb: FormBuilder,
                private message: NzMessageService,
                private fetchService: FetchService) {
        this.searchForm = this.fb.group({
            rangePickerTime: [[]],
            deptName: [''],
            position: ['']
        });
        this.form = this.fb.group({
            id: [null],
            deptName:  [null, Validators.required],
            userName:  [null, Validators.required],
            position:  [null, Validators.required],
            inspectTime:  [null, Validators.required],
            problem:  [null, Validators.required],
            measures:  [null, Validators.required],
            effect:  [null, Validators.required],
            remark:  [null, Validators.required]
        });
    }

    ngOnInit(): void {
        this.queryInfo();
    }

    queryInfo(reset: boolean = false): void {
        if (reset) {
            this.pageNum = 1;
        }
        this.loading = true;
        const params = {
            type: '1',  // 1-定期安全检查，type = 2-日常安全检查
            stTime: this.searchForm.value.rangePickerTime[0],
            edTime: this.searchForm.value.rangePickerTime[1],
            deptName: this.searchForm.value.deptName,
            position: this.searchForm.value.position,
            pageSize: this.pageSize,
            pageNum: this.pageNum
        };
        this.fetchService.post('safeProduct/wrsSafeProductInspect/list', params).subscribe(res => {
            this.tableData = res['list'];
            this.total = res['total'];
            this.loading = false;
        });
    }

    openEditModal(isEditable: boolean = true, item?): void {
        this.modalTitle = isEditable ? (item ? `编辑` : '新增') : '详情';
        this.queryType = isEditable ? "" : null;

        this.form.patchValue(item || {});
        if (isEditable) {
            this.form.enable();
        } else {
            this.form.disable();
        }
        this.modalShow = true;
    }

    onDelete(id) {
        this.fetchService.get(`safeProduct/wrsSafeProductInspect/remove/${id}`).subscribe(resp => {
            this.message.info('删除成功');
            this.queryInfo();
        });
    }

    onConfirm() {
        if (!this.form.valid) {
            this.message.warning('信息填写有误');
            return;
        }
        const data = {
            ...this.form.value,
            ...{ type: '1' }
        };
        const url = data.id ? 'safeProduct/wrsSafeProductInspect/edit' : 'safeProduct/wrsSafeProductInspect/add';
        this.fetchService.post(url, data).subscribe(res => {
            this.message.success('提交成功');
            this.modalShow = false;
            this.queryInfo();
            this.form.reset();
        });
    }

    onCancel() {
        this.form.reset();
        this.modalShow = false;
    }
}
